/*********************************************************************************
 *      Copyright:  (C) 2017 luminqi
 *                  All rights reserved.
 *
 *       Filename:  bubble_sort.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(2017年08月26日)
 *         Author:  luminqi <958822582@qq.com>
 *      ChangeLog:  1, Release initial version on "2017年08月26日 00时21分48秒"
 *                 
 ********************************************************************************/

#include <stdio.h>

void buddle_sort(int a[], int n);
int main(int argc, char **argv)
{
    int a[7] = {5,6,7,4,3,2,1};
    int i = 0;
    printf("before sort:\n");
    for (i= 0; i<7; i++)
    {   
         printf("%d ",a[i]);
    }
    printf("\n");

    printf("after sort:\n");
    buddle_sort(a, 7);
    for (i= 0; i<7; i++)
    {
        printf("%d ",a[i]);
    }
    printf("\n");

    return 0;
}

void buddle_sort(int a[], int n)
{
    int i = 0;
    int j = 0;
    int tmp = 0;

    for (i=1; i<n; i++)
    {   //最坏情况要n-1趟排序
        for (j=0; j<n-i; j++)  //比较次数：第一趟n-1次，则第i趟n-i次
        {
            if(a[j] > a[j+1])
            {
                tmp = a[j];
                a[j] = a[j+1];
                a[j+1] = tmp;
            }
        }
    }
    return;
}
